<template>
  <div style="padding: 40px">
    <el-container>
      <el-row style="width: 100%">
        <el-col v-for="index in worksList" :span="5" :offset="1" pull="1" style="width: 290px">
          <div v-if="index.type === 3" class="card_div">
            <el-card :body-style="{ padding: '0px'}">
              <div>
                <div class="card-top">
                  <el-image :src="index.url" class="image" fit="fill" />
                </div>
                <div class="message">
                  <div class="bottom">
                    <time class="type">照片</time>
                    <el-button type="text" class="button" @click="openViewer(index.url)">查看照片</el-button>
                    <el-image-viewer
                      v-if="showViewer"
                      :on-close="closeViewer"
                      :url-list="[url]"
                    />
                    <el-button slot="reference" type="text" class="button_ch"  @click="showChangeGrade(index.wid, index.grade)">{{index.grade}}</el-button>
                    <i v-if="index.isOnHomePage==1" class="el-icon-star-on icon"></i>
                    <i v-else class="el-icon-star-off icon" @click="showIn(index.wid)"></i>
                  </div>
                </div>
              </div>
            </el-card>
          </div>
          <div v-else-if="index.type === 2" class="card_div"">
            <el-card :body-style="{ padding: '0px' }">
              <div>
                <div class="card-top">
                  <el-image :src="require('../../../assets/work_pic/video_pic.jpg')" fit="fill" class="image" />
                </div>
                <div class="message">
                  <div class="bottom ">
                    <time class="type">视频</time>
                    <el-button type="text" class="button" @click="videoPlay">播放视频</el-button>
                    <el-dialog title="视频播放" :visible.sync="dialogVideoPlayer" top="30px" width="70%" style="height: 570px">
                      <video :ref="dialogVideoPlayer" :src="index.url" controls class="video" />
                    </el-dialog>
                    <el-button slot="reference" type="text" class="button_ch"  @click="showChangeGrade(index.wid, index.grade)">{{index.grade}}</el-button>
                    <i v-if="index.isOnHomePage==1" class="el-icon-star-on icon"></i>
                    <i v-else class="el-icon-star-off icon" @click="showIn(index.wid)"></i>
                  </div>
                </div>
              </div>
            </el-card>
          </div>
          <div v-else-if="index.type === 1" class="card_div"">
            <el-card :body-style="{ padding: '0px' }">
              <div>
                <div class="card-top">
                  <el-image :src="require('../../../assets/work_pic/music_pic.jpg')" fit="fill" class="image" opacity />
                </div>
                <div class="message">
                  <div class="bottom">
                    <time class="type">音频</time>
                    <el-popover
                      style="padding-left: 10px"
                      placement="bottom"
                      title="播放"
                      width="400px"
                      trigger="click"
                    >
                      <audio :src="index.url" controls="controls" />
                      <el-button slot="reference" type="text" class="button">播放音频</el-button>
                    </el-popover>
                    <el-button slot="reference" type="text" class="button_ch"  @click="showChangeGrade(index.wid, index.grade)">{{index.grade}}</el-button>
                    <i v-if="index.isOnHomePage==1" class="el-icon-star-on icon"></i>
                    <i v-else class="el-icon-star-off icon" @click="showIn(index.wid)"></i>
                  </div>
                </div>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
      <el-dialog
        title="修改作品等级"
        :visible.sync="dialogFormVisible"
        width="50%">
        <div style="padding-left: 10%">
          <template>
            <el-radio v-model="gradeOp" label="1">优秀</el-radio>
            <el-radio v-model="gradeOp" label="2">良好</el-radio>
            <el-radio v-model="gradeOp" label="3">中等</el-radio>
            <el-radio v-model="gradeOp" label="4">及格</el-radio>
            <el-radio v-model="gradeOp" label="5">不及格</el-radio>
            <el-button type="primary" @click="changeGrade">确认修改</el-button>
          </template>
        </div>
      </el-dialog>
    </el-container>
    <el-pagination
      layout="prev, pager, next"
      hide-on-single-page="true"
      :current-page="page.pageNum"
      :page-size="page.pageSize"
      :total="page.totalSize"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import {getWorkLimt, updateWorkGrade,showIndex} from '../../../api/works'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
  name: 'Works',
  components: { ElImageViewer },
  data() {
    return {
      gradeOp: 4,
      dialogFormVisible: false,
      baseUrl: this.global.picBaseUrl,
      dialogVideoPlayer: false,
      music_pic: require('../../../assets/work_pic/music_pic.jpg'),
      showViewer: false, // 显示查看器
      url: '',
      worksList: [],
      page: {
        pageNum: 1,
        pageSize: 12,
        totalPage: 0,
        totalSize: 0
      }
    }
  },
  mounted() {
    this.$refs.btn.onclik(function() {
      if (this.$refs.dialogVideo.paused) {
        this.$refs.dialogVideo.play()
      } else {
        this.$refs.dialogVideo.pause()
      }
    })
  },
  created() {
    this.getWorkList()
  },
  methods: {
    // 点击显示在首页
    showIn(wid) {
      showIndex(wid)
        .then(res => {
          this.getWorkList()
          this.$message.success('成功设置首页显示！')
        })
    },
    // 显示修改等级页面
    showChangeGrade(wid, grade) {
      this.dialogFormVisible = true
      this.wid = wid
      console.log('grade', grade)
      switch (grade) {
        case '未设置':
          this.gradeOp = 0;
          break;
        case '优秀':
          this.gradeOp = 1;
          break;
        case '良好':
          this.gradeOp = 2;
          break;
        case '中等':
          this.gradeOp = 3;
          break;
        case '及格':
          this.gradeOp = 4;
          break;
        case '不及格':
          this.gradeOp = 5;
          break;
      }
      console.log('gradeop', this.gradeOp)
    },
    // 改变等级
    changeGrade() {
      console.log('grade', this.gradeOp)
      updateWorkGrade(this.wid, { grade: this.gradeOp })
        .then(res => {
          this.$message.success('修改作品等级成功！')
          this.getWorkList()
          this.dialogFormVisible = false
        })
    },
    // 图片预览
    openViewer(url) {
      this.url = url
      this.showViewer = true
    },
    // 关闭预览
    closeViewer() {
      this.showViewer = false
    },
    // 视频播放
    videoPlay() {
      this.dialogVideoPlayer = true
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getWorkList()
    },
    // 获取作品列表
    getWorkList() {
      let i = 0
      getWorkLimt({
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize
      }).then(res => {
        this.worksList = res.data.list
        this.page = res.data.page
        for (i = 0; i < this.worksList.length; i++) {
          this.worksList[i].url = this.baseUrl + this.worksList[i].url
          // this.worksList[i].url = 'http://192.168.43.101' + this.worksList[i].url
          switch (this.worksList[i].grade) {
            case 0:
              this.worksList[i].grade = '未设置';
              break;
            case 1:
              this.worksList[i].grade = '优秀';
              break;
            case 2:
              this.worksList[i].grade = '良好';
              break;
            case 3:
              this.worksList[i].grade = '中等';
              break;
            case 4:
              this.worksList[i].grade = '及格';
              break;
            case 5:
              this.worksList[i].grade = '不及格';
              break;
          }
        }
        console.log('list', this.worksList)
      }).catch(err => {
        console.log('err', err)
      })
    }
  }
}
</script>

<style scoped>
  .card_div{
    height: 290px;
  }
  .type {
    font-size: 13px;
    color: #999;
  }

  .bottom {
    margin-top: 13px;
    line-height: 12px;
    float: bottom;
  }
  .message {
    padding: 14px;
    margin-bottom: 2px;
  }
  .button {
    margin-right: 0px;
    padding: 0;
    float: right;
  }
  .button_ch {
    padding: 0;
    margin-right: 30px;
    float: right;
  }
  .icon {
    padding: 0;
    margin-right: 50px;
    float: right;
  }
  .image {
    height: 150px;
    display: block;
    width: 290px;
  }
  .video {
    display: block;
    margin: 0 auto;
    height: 400px;
  }
  .card-top {
    height: 150px;
    width: 290px;
  }
</style>
